local Updater = Class(function(self, inst)
    self.inst = inst 

    self.maxlevel = 30
    self.level = 0
    self.iv = {}
    self.prlv = {}

end)

function Updater:Init(data)
    for iv, ranges in pairs(data.iv) do
        self.iv[iv] = { 
            grow = math.random(ranges.grow[1]*100,range.grow[2]*100)/100,
            start = math.random(ranges.start[1]*100,range.start[2]*100)/100,
        }
    end
    for skill, pr in pairs(data.pr) do 
        self.prlv[skill] = {pr = pr.pr, v = pr.val, lv = 0}
    end
    return self.iv
end

function Updater:SetLevelUpFn(fn)
    self.onlevelupfn = fn
end

function Updater:LevelUp(levels, source)
    levels = levels or 1
    for i = 1, levels do
        if self.level >= self.maxlevel then break end
        self.level = self.level + 1
        local data = {lv = self.level, iv = self.iv, pr = {}}
        for skill, pr in pairs(self.prlv) do 
            local p = math.random()
            if p <= pr then 
                pr.lv = pr.lv + 1
                data.pr[skill] = {val = pr.v, lv = pr.lv}
            end
        end
        if self.onlevelupfn then 
            self.onlevelupfn(self.inst, data, source)
        end
        inst:PushEvent("levelup", {data = data, source = source})
    end
end

function Updater:OnSave()
    local data = {}
    data.level = self.level 
    data.iv = self.iv 
    data.priv = self.priv
    return data 
end

function Updater:OnLoad(data)
    self.level = data.level 
    self.iv = data.iv 
    self.priv = data.priv
end

return Updater 